package com.ygq.train.member.service.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.ygq.train.common.aspect.LogAspect;
import com.ygq.train.common.exception.BusinessException;
import com.ygq.train.common.exception.BusinessExceptionEnum;
import com.ygq.train.common.resp.MemberLoginResp;
import com.ygq.train.common.util.JwtUtil;
import com.ygq.train.common.util.SnowUtil;
import com.ygq.train.member.domain.Member;
import com.ygq.train.member.domain.MemberExample;
import com.ygq.train.member.mapper.MemberMapper;
import com.ygq.train.member.req.MemberLoginReq;
import com.ygq.train.member.req.MemberRegisterReq;
import com.ygq.train.member.req.MemberSendCodeReq;
import com.ygq.train.member.service.MemberService;
import jakarta.annotation.Resource;
import org.apache.commons.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class MemberServiceImpl implements MemberService {
    private final static Logger LOG = LoggerFactory.getLogger(LogAspect.class);

    @Resource
    private MemberMapper memberMapper;

    @Override
    public int count() {
        return Math.toIntExact(memberMapper.countByExample(null));
    }


    @Override
    public Long Register(MemberRegisterReq req) {


        String mobile = req.getMobile();
        Member memberDB = SelectByMobile(mobile);

        if (ObjectUtil.isNotNull(memberDB)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        member.setId(new SnowUtil().getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();
    }

    @Override
    public void SendCode(MemberSendCodeReq req) {
        String mobile = req.getMobile();
        Member memberDB = SelectByMobile(mobile);

        if (ObjectUtil.isNull(memberDB)) {
            LOG.info("手机号不存在，注册到数据库");
            Member member = new Member();
            member.setId(new SnowUtil().getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }else{
            LOG.info("手机号存在，不插入数据");
        }

        //生成验证码
//       String code= RandomUtil.randomString(4);
        String code="8888";//方便测试，设置为8888
        LOG.info("手机号存在，生成短信成验证码：{}",code);

        LOG.info("保存短信记录表：手机号/验证码/有效时间/是否使用/业务类型/发生时间/使用时间");
        //保存短信记录表：手机号/验证码/有效时间/是否使用/业务类型/发生时间/使用时间
        //考虑使用redis
        LOG.info("对接短信通道，发送短信");
        //对接短信通道，发送短信
    }

    @Override
    public  MemberLoginResp Login(MemberLoginReq req) {
        String mobile = req.getMobile();
        String code = req.getCode();
        Member memberDB = SelectByMobile(mobile);


        if (ObjectUtil.isNull(memberDB)) {
            LOG.info("手机号不存在，注册到数据库");
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }
        // 校验短信验证码
        if (!"8888".equals(code)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }
        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);
        String token= JwtUtil.createToken(memberLoginResp.getId(),memberLoginResp.getMobile());
        memberLoginResp.setToken(token);
        return memberLoginResp;
    }

    private Member SelectByMobile(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);
        if (CollUtil.isEmpty(members)) {
            return null;
        } else {
            return members.get(0);
        }
    }

}
